<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataStation2 extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $db = Db::name('data_station2');
        if(!empty($param['kw'])){
            $db = $db->where('name','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        return view('',['list'=>$list,'count'=>$count,'param'=>$param]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = Db::name('data_station2')->insert($param);
            if( $result ) {
                xn_add_admin_log('添加火车站');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = Db::name('data_station2')->update($param);
            if( $result ) {
                xn_add_admin_log('修改火车站');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        $data = Db::name('data_station')->where('id',$id)->find();
        return view('form',['data'=>$data]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_station2')->delete($id);
        xn_add_admin_log('删除火车站');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        Db::name('data_station2')->delete($ids);
        xn_add_admin_log('批量删除火车站');
        $this->success("删除成功");
    }


}
